﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using FileManagerGlobal_Library;
using FileManager_Server.BusinessLogic.IRepository_Service;
using FileManager_Server.BusinessLogic.WCF.ObjectContainer_Setting;
using FileManager_Server.BusinessLogic.WCF.System_Setting_Service;
using FileManager_Server.ServiceModel.Client_Update_Model.Client_Update_DataTransfer_Model;
using FileManager_Server.ServiceModel.DataTransfer_Model;
using FileManager_Server.ServiceModel.WCF_Args_Model;
using FileManager_Server.Utility;

namespace FileManager_Server.BusinessLogic.WCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "FileManager_UserService" in both code and config file together.
    //[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]会为客户端的每次请求分配并销毁一个新的服务实例
    //[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]会为每次客户端连接分配一个服务实例
    //[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]所有的客户端的所有连接会共享一个相同的服务实例
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class FileManager_UserService : IFileManager_UserService
    {

        public UserLoginResult Login(UserLoginArgs userLoginArgs)
        {
            var userService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IUserService>();
            return userService.Login(userLoginArgs);
        }


        public FolderResultCollection GetDepartmentFolder()
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.GetDepartmentFolder();
        }


        public CompanyResultCollection GetCompanyCollection()
        {
            var userService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IUserService>();
            return userService.GetCompanyCollection();
        }


        public FolderRoleResultCollection GetFolderRoleCollection(FolderRoleQueryArgs folderRoleQueryArgs)
        {
            var userService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IUserService>();
            return userService.GetFolderRoleCollection(folderRoleQueryArgs);
        }


        public FileResultCollection GetFolderFileCollection(FileQueryArgs fileQueryArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.GetFolderFileCollection(fileQueryArgs);
        }


        public FileDeleteResult DeleteFileService(FileDeleteArgs fileDeleteArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.DeleteFileService(fileDeleteArgs);
        }


        public FileReNameResult ChangeFileNameService(FileReNameArgs fileReNameArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.ChangeFileNameService(fileReNameArgs);
        }

        public FolderReNameResult ChangeFolderNameService(FolderReNameArgs folderReNameArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.ChangeFolderNameService(folderReNameArgs);
        }


        public FolderDeleteResult DeleteFolderService(FolderDeleteArgs folderDeleteArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.DeleteFolderService(folderDeleteArgs);
        }


        public FileCopyToResult CopyFileToOtherFolder(FileCopyToArgs fileCopyToArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.CopyFileToOtherFolder(fileCopyToArgs);
        }

        public FileCutToResult CutFileToOtherFolder(FileCutToArgs fileCutToArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.CutFileToOtherFolder(fileCutToArgs);
        }

        public FolderCopyToResult CopyFolderToOtherFolder(FolderCopyToArgs folderCopyToArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.CopyFolderToOtherFolder(folderCopyToArgs);
        }

        public FolderCutToResult CutFolderToOtherFolder(FolderCutToArgs folderCutToArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.CutFolderToOtherFolder(folderCutToArgs);
        }


        public NewFolderResult CreateNewFolder(NewFolderArgs newFolderArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.CreateNewFolder(newFolderArgs);
        }

        public UserResultCollection SearchUserByName(SearchUserNameArgs searchUserNameArgs)
        {
            var userService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IUserService>();
            return userService.SearchUserByName(searchUserNameArgs);
        }


        public CompanGroupAndUserGroupResultCollection GetCompanyGroupAndUserGroupCollection()
        {
            var userService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IUserService>();
            return userService.GetCompanyGroupAndUserGroupCollection();
        }


        public UserResultCollection SearchUserByCompanyCode(SearchUserByCompanyCodeArgs searchUserByCompanyCodeArgs)
        {
            var userService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IUserService>();
            return userService.SearchUserByCompanyCode(searchUserByCompanyCodeArgs);
        }


        public FileRoleAddResultCollection GetFolderRole(FileRoleAddArgs fileRoleAddArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.GetFolderRole(fileRoleAddArgs);
        }


        public NewFileRoleResultCollection NewFileRole(NewFileRoleCollectionArgs newFileRoleCollectionArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.NewFileRole(newFileRoleCollectionArgs);
        }


        public FileRoleChangeResult ChangeFileRole(FileRoleChangeArgs fileRoleChangeArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.ChangeFileRole(fileRoleChangeArgs);
        }


        public ApplySubFolderRoleResult ApplySubFolderRole(ApplySubFolderRoleArgs applySubFolderRoleArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.ApplySubFolderRole(applySubFolderRoleArgs);
        }


        public DeleteFileRoleResult DeleteFileRole(DeleteFileRoleArgs deleteFileRoleArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.DeleteFileRole(deleteFileRoleArgs);
        }


        public FileLogQueryCountResult GetFileLogCount(FileLogQueryArgs fileLogQueryArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.GetFileLogCount(fileLogQueryArgs);
        }

        public FileLogResultCollection GetFileLog(FilelogQueryPageArgs filelogQueryPageArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.GetFileLog(filelogQueryPageArgs);
        }


        public FileRoleAddResultCollection GetFolderRoleDelegate(FileRoleAddDDelegateArgs fileRoleAddDDelegateArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.GetFolderRoleDelegate(fileRoleAddDDelegateArgs);
        }


        public FileResultCollection SearchFile(FileSearchArgs fileSearchArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.SearchFile(fileSearchArgs);
        }


        public CheckFileResult CheckFile(CheckFileUploadingArgs checkFileUploadingArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.CheckFile(checkFileUploadingArgs);
        }


        public void DeleteUploadingFile(DeleteUploadingFileArgs deleteUploadingFileArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            fileService.DeleteUploadingFile(deleteUploadingFileArgs);
        }


        public DownloadingCheckFileResultCollection CheckFileInfomation(DownlaodingCheckFilesArgs downlaodingCheckFilesArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return  fileService.CheckFileInfomation(downlaodingCheckFilesArgs);
        }


        public void DownloadingFileSuccess(DownloadingSuccessArgs downloadingSuccessArgs)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            fileService.DownloadingFileSuccess(downloadingSuccessArgs);
        }


        public DepartmentSizeResultCollection GetDepartmentSizeCollection()
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.GetDepartmentSizeCollection();
        }

        public int GetDepartmentSizeMB(string departmentId)
        {
            var fileService = ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<IFileService>();
            return fileService.GetDepartmentSizeMB(departmentId);
        }


        public UpdateFileInfo CheckClientUpdateInfo()
        {
            var result = new UpdateFileInfo();
            try
            {
                result.FileName = GlobalVariable.ClientAppVariable.ClientUpdateFileName;
                var applicationInfo = ServerLocalFile.ReadApplicationInfo();
                if (null != applicationInfo)
                {
                    result.ApplicationVersion = applicationInfo.ClientVersion;
                    var updatePath = Path.Combine(Application.StartupPath,
                                              GlobalString.System_Config_String.ClientUpdatePath);
                    if(File.Exists(updatePath))
                    {
                        var fileInfo = new FileInfo(updatePath);
                        result.FileLength = fileInfo.Length;
                    }
                    result.UpdateInfomation = applicationInfo.Infomation;
                }
                else
                    result.UpdateError = UpdateError.NotFindFile;
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("CheckClientUpdateInfo has problem", ex);
                result.UpdateError = UpdateError.ServerHasProblem;
            }
            return result;
        }
    }
}
